<?php

class NConfigFileCreator
{
    public static function getFile()
    {
        $features = self::checkFeatures();

        $content = <<<EOD
/*
 * Mode must match a value in first array key.
 * The framework settings include:
 *
 * 'error_handling'
 *      N::DISPLAY_ERRORS               - Displays the error/exception message along with the stack trace.
 *		N::SUPPRESS_ERRORS              - Shows the default 'error_page'.
 *
 *
 * 'error_page'
 *      Page displayed when an unhandled error/exception occurs and 'error_handling' is set to N::SUPPRESS_ERRORS
 *
 *
 * 'profiling'
 *		N::APPEND_PROFILING_RESULTS     - Times for markers added to page are displayed as commented lines at the end of the script.
 *		N::PROFILING_OFF                - Profiling is turned off.
 *
 *
 * 'debug'
 * 		N::DEBUG_OFF                    - No debug information displayed
 * 		N::APPEND_DEBUG_INFO            - Append debug system status info just before body tag of page
 *
 *
 * 'sql_action_dsn'
 * 		Default DSN for SQL Action (e.g., insert, update, delete) database connections.
 *
 *
 * 'sql_action_username'
 * 		Default username for SQL Action (e.g., insert, update, delete) database connections
 *
 *
 * 'sql_action_password'
 * 		Default password for SQL Action (e.g., insert, update, delete) database connections
 *
 *
 * 'sql_datasource_dsn'
 * 		Default DSN for SQL Datasource (e.g., select) database connections.
 *
 *
 * 'sql_datasource_username'
 * 		Default DSN for SQL Datasource (e.g., select) database connections.
 *
 *
 * 'sql_datasource_password'
 * 		Default DSN for SQL Datasource (e.g., select) database connections.
 *
 *
 * 'encryption_key'
 *		Key used by your site for encryption.
 *
 *
 * 'encryption_salt'
 *		Salt used by hashing algorithms within the encryption process.
 *
 *
 * 'file_ftp_url'
 *		The file ftp url (preferably for an account that only has access to the directory file uploads, writes, and deletes will occur) that follows the format below:
 *		ftp://USERNAME:PASSWORD@ftp.YOURDOMAIN.com
 *
 *		Used when file operations can't be performed without making a directory world writeable.
 *
 *
 * 'base_url_directory'
 * 		Used to set the base url directory of a site, for instance if the DNS has not propagated yet and you have to access
 *		the site at http://servername.hostname.com/~username, you'd set the base url directory to '/~username'
 *
 *
 * 'default_timezone'
 *		One way to set the default timezone used by all date/time functions in Nephtali pages (you can also use the INI setting date.timezone to set the default timezone for all PHP scripts.)
 *		The possible values are found at: http://us2.php.net/manual/en/timezones.php
 *
 *
 * 'protected_directories'
 * 		Sets an array of directories that require a particular session variable as an authentication token.
 *  	The example below would protect the '/admin' directory, require an 'admin_id' session variable, and redirect unauthorized requests to 'http://example.com/login.php'.
 *
 *      array(
 *			'/admin'=>array('required_session_var'=>'admin_id', 'login'=>'http://example.com/login.php')
 *		)
 *
 *
 * 'integrated_pages'
 *      Sets an array of directories with their corresponding integrated pages.
 *      The example below would integrate the pages NMod_AdminPresentation_Page and NMod_SpecialFormatting_Page into all pages within the /admin directory.
 *
 *      array(
 *          '/admin'=>array('NMod_AdminPresentation_Page', 'NMod_SpecialFormatting_Page')
 *      )
 *
 *
 * The 'default' settings are:
 * 'error_handling'             = N::SUPPRESS_ERRORS;
 * 'sessions'                   = N::SESSIONS_OFF;
 * 'error_page'                 = 'error.html';
 * 'profiling'                  = N::PROFILING_OFF;
 * 'debug'                      = N::DEBUG_OFF;
 * 'encryption_key'             = NULL;
 * 'encryption_salt'            = NULL;
 * 'file_ftp_url'               = NULL;
 * 'base_url_directory'         = '';
 * 'default_timezone'           = NULL;
 * 'sql_action_dsn'             = NULL;
 * 'sql_action_username'        = NULL;
 * 'sql_action_password'        = NULL;
 * 'sql_datasource_dsn'         = NULL;
 * 'sql_datasource_username'    = NULL;
 * 'sql_datasource_password'    = NULL;
 * 'protected_directories'      = array();
 * 'integrated_pages'           = array();
 *
 * You can add whatever settings you'd like and use N::config('your_setting_name') to access the value throughout the site.
 * Below, search_app_id is an example of a custom setting.
 */

NCore_Utility_ConfigManager::setMode('development');

EOD;

        $dev = "\$nephtali_config['development'] = array(\n";
        $prod = "\$nephtali_config['production']  = array(\n";

        if (!is_null($features['base_dir'])) {
            $dir = $features['base_dir'];

            $prod .=<<<EOD
    'base_url_directory'					=> '$dir', // --- Added 'base_url_directory' setting.  Often, this has to be used while you're viewing the site with a temporary url ---

EOD;

			$dev .=<<<EOD
    'base_url_directory'					=> '$dir',	// --- Added 'base_url_directory' setting.  Often, this has to be used while you're viewing the site with a temporary url ---

EOD;
		}

        if (!$features['encryption']) {

            $unsupported =<<<EOD
    // 'encryption_key'                     => "key", 																--- CURRENTLY UNSUPPORTED ON THIS SERVER ---
    // 'encryption_salt'                    => "salt",																--- CURRENTLY UNSUPPORTED ON THIS SERVER ---

EOD;
            $dev .= $unsupported;
            $prod .= $unsupported;
        } else {
            $supported =<<<EOD
    'encryption_key'                        => "key",
    'encryption_salt'                       => "salt",

EOD;
            $dev .= $supported;
            $prod .= $supported;
        }

        if (!$features['pdo']) {
            $unsupported =<<<EOD
    // 'sql_action_dsn'                     => 'mysql:host=localhost;dbname=DBNAME', 	--- CURRENTLY UNSUPPORTED ON THIS SERVER ---
    // 'sql_action_username'                => 'USERNAME',														--- CURRENTLY UNSUPPORTED ON THIS SERVER ---
    // 'sql_action_password'                => 'PASSWORD',														--- CURRENTLY UNSUPPORTED ON THIS SERVER ---
    // 'sql_datasource_dsn'                 => 'mysql:host=localhost;dbname=DBNAME',	--- CURRENTLY UNSUPPORTED ON THIS SERVER ---
    // 'sql_datasource_username'            => 'USERNAME',														--- CURRENTLY UNSUPPORTED ON THIS SERVER ---
    // 'sql_datasource_password'            => 'PASSWORD',														--- CURRENTLY UNSUPPORTED ON THIS SERVER ---

EOD;
            $dev .= $unsupported;
            $prod .= $unsupported;
        } else {
            $supported =<<<EOD
    'sql_action_dsn'                        => 'mysql:host=localhost;dbname=DBNAME',
    'sql_action_username'                   => 'USERNAME',
    'sql_action_password'                   => 'PASSWORD',
    'sql_datasource_dsn'                    => 'mysql:host=localhost;dbname=DBNAME',
    'sql_datasource_username'               => 'USERNAME',
    'sql_datasource_password'               => 'PASSWORD',

EOD;

            $dev .= $supported;
            $prod .= $supported;
        }

        $dev .=<<<EOD
    'error_handling'                        => N::DISPLAY_ERRORS,
    'profiling'                             => N::APPEND_PROFILING_RESULTS,
    'debug'                                 => N::APPEND_DEBUG_INFO,

EOD;

        $prod .= "  'search_app_id'                             => 'SEARCH_KEY'\n);";
        $dev .= "   'search_app_id'                             => 'SEARCH_KEY'\n);";

        $content .= "\n\n$prod\n\n$dev";

        return "<?php\n".$content."\n?>";
    }

    public static function checkFeatures()
    {

        $features = array();

        if ($_SERVER['PHP_SELF'] != "/setup/download.php") {
            $features['base_dir'] = str_replace('/setup/download.php', '', $_SERVER['PHP_SELF']);
        } else {
            $features['base_dir'] = NULL;
        }

        if (!extension_loaded('pdo')) {
            $features['pdo'] = FALSE;
        } else {
            $features['pdo'] = TRUE;
        }

        if (function_exists('mcrypt_list_algorithms') && function_exists('hash') && function_exists('hash_hmac')) {
            $encryption = false;

            foreach (mcrypt_list_algorithms() as $cipher) {
                if ($cipher == 'rijndael-256') {
                    $encryption = true;
                    break;
                }
            }

            if (!$encryption) {
                $features['encryption'] = FALSE;
            } else {
                $features['encryption'] = TRUE;
            }
        } else {
          $features['encryption'] = TRUE;
        }

        return $features;
    }
}

?>